package com.example.administrator.im.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.example.administrator.im.Contents;
import com.example.administrator.im.R;
import com.example.administrator.im.adapter.ApplyAndNotificationAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：wp
 * 时间：2016/4/19 16:20
 * 描述：TODO
 */
public class ApplyAndNotificationActivity extends Activity implements ApplyAndNotificationAdapter.btnOnClick {
    @Bind(R.id.tv_title_middle)
    TextView tvTitleMiddle;
    @Bind(R.id.apply_notif_listview)
    ListView applyNotifListview;
    private ApplyAndNotificationAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_notifi);
        ButterKnife.bind(this);
        tvTitleMiddle.setText("好友申请与通知");
        if(adapter == null) {
            adapter = new ApplyAndNotificationAdapter(this);
            applyNotifListview.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        adapter.setOnClickBtnListener(this);
    }

    @OnClick(R.id.iv_title_left)
    public void onClick() {
        Contents.lists.clear();
        finish();
    }

    /**
     * 同意好友申请
     * @param username
     */
    @Override
    public void onClickAgreeBtn(final String username, final Button btn1, final Button btn2, final TextView tv) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //同意username的好友请求
                try {
                    EMChatManager.getInstance().acceptInvitation(username);//需异步处理
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            btn1.setVisibility(View.GONE);
                            btn2.setVisibility(View.GONE);
                            tv.setVisibility(View.VISIBLE);
                            tv.setText("已同意");
                        }
                    });
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 拒绝好友申请
     * @param username
     */
    @Override
    public void onClickRefuseBtn(final String username, final Button btn1, final Button btn2, final TextView tv) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //拒绝username的好友请求
                try {
                    EMChatManager.getInstance().refuseInvitation(username);//需异步处理
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            btn1.setVisibility(View.GONE);
                            btn2.setVisibility(View.GONE);
                            tv.setVisibility(View.VISIBLE);
                            tv.setText("已拒绝");
                        }
                    });
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
